<template>
  <div>
    <page-list :x-uri="uri" :doactions="doactions" :custom-components="customComponents" :checkbox-item="false">
      <!-- 自定义tab -->
      <template v-slot:page-tabs>
        <tabs />
      </template>
      <!-- 页面操作按钮 -->
      <template v-slot:page-button>
        <el-button v-auth="'exam.module.create'" size="mini" type="primary" @click="create">新建版块</el-button>
      </template>
    </page-list>
  </div>
</template>
<script>
import PageList from '@public/template/page_list.vue';
import Tabs from './tabs';
import Status from './components/status.vue';
export default {
  components: {
    PageList,
    Tabs
  },
  inject: ['reload'],
  data() {
    return {
      uri: '/exam/admin/module/list',
      customComponents: {
        status: Status
      },
      doactions: [
        { title: '版块配置', meta: { permission: 'exam.module.config' }, callback: this.config },
        { title: '编辑', meta: { permission: 'exam.module.edit' }, callback: this.edit },
        {
          title: '删除',
          meta: { permission: 'exam.module.delete' },
          callback: this.delete,
          display: ({ row }) => {
            return row.id > 4;
          }
        }
      ]
    };
  },
  methods: {
    // 新建
    create: function() {
      this.$router.push({ path: '/exam/module/create' });
    },
    // 配置
    config: function({ row }) {
      const moduleType = {
        1: 'point',
        2: 'special',
        3: 'examination',
        4: 'rollup'
      };
      this.$router.push({ path: '/exam/module/' + row.id + '/' + moduleType[row.module_type] + '/config' });
    },
    // 编辑
    edit: function({ row }) {
      this.$router.push({ path: '/exam/module/' + row.id + '/edit' });
    },
    // 删除
    delete: function({ row }) {
      this.$box
        .confirm('确认删除吗')
        .then(() => {
          this.$http.delete(
            '/exam/admin/module/' + row.id + '/delete',
            {},
            response => {
              this.$message.success(response.msg, _ => {
                this.reload();
              });
            },
            error => {
              this.$message.error(error.msg);
            }
          );
        })
        .catch(() => {});
    }
  }
};
</script>
